package com.hexinfo.dmpro.monitor.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hexinfo.dmpro.monitor.config.MonitorCache;
import com.hexinfo.dmpro.monitor.dao.HdfsResourceDao;
import com.hexinfo.dmpro.monitor.model.HdfsResourceModel;
import com.hexinfo.dmpro.monitor.model.base.MonitorReq;
import com.hexinfo.dmpro.monitor.model.base.MonitorRsp;
import com.hexinfo.dmpro.monitor.model.HdfsResourceModel;
import com.hexinfo.dmpro.monitor.model.base.MonitorReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author peng
 * @date 2022/1/10 10:43
 */
@Slf4j
@Service
public class HdfsService {

    private int SUCESS_CODE = 200;

    @Autowired
    private HdfsJmxService hdfsJmxService;

    @Autowired
    private HdfsResourceDao hdfsResourceDao;

    /**
     * 获取hdfs 总共使用资源
     *
     * @param req
     * @param flag
     * @return
     */
    public HdfsResourceModel getHdfsAllResource(MonitorReq req, boolean flag) {
        List<MonitorReq> monitorReqList = MonitorCache.getMonitorReq(req.getCenterId(), req.getCmFlag(), req.getClusterFlag(), MonitorParam.HDFS_RESOURCE);
        List<HdfsResourceModel> hdfsResourceModels = new ArrayList<>();
        HdfsResourceModel hdfsResourceModel = null;
        log.info("getHdfsAllResource-------");
        if (monitorReqList != null && monitorReqList.size() > 0) {
            for (MonitorReq monitorReq : monitorReqList) {
                req.setCenterName(monitorReq.getCenterName());
                req.setClusterName(monitorReq.getClusterName());
                String url = monitorReq.getUrl();
                url += "?qry=Hadoop:service=NameNode,name=FSNamesystem";
                monitorReq.setUrl(url);
                MonitorRsp rsp = hdfsJmxService.execute(monitorReq);
                if (SUCESS_CODE == rsp.getCode()) {
                    JSONArray jsonArray = (JSONArray) rsp.getObj();
                    log.info("getHdfsAllResource-------jsonObject");
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    hdfsResourceModel = new HdfsResourceModel();
                    hdfsResourceModel.setTotalGB(jsonObject.getString("CapacityTotalGB"));
                    hdfsResourceModel.setUsedGB(jsonObject.getString("CapacityUsedGB"));
                    hdfsResourceModel.setTotalB(jsonObject.getString("CapacityTotal"));
                    hdfsResourceModels.add(hdfsResourceModel);
                    break;
                }
            }
        }
        if (flag && hdfsResourceModels != null && hdfsResourceModels.size() > 0) {
            log.info("getHdfsAllResource-------flag");
            Map<String, Object> map = new HashMap<>();
            map.put("list", hdfsResourceModels);
            map.put("req", req);
            hdfsResourceDao.insertAllResource(map);

            hdfsResourceDao.deleteAllResourceNow(map);
            hdfsResourceDao.insertAllResourceNow(map);

        }
        return hdfsResourceModel;
    }

    /**
     * 获取hdfs用户使用资源
     *
     * @param req
     * @param flag
     * @return
     */
    public Object getHdfsResource(MonitorReq req, boolean flag) {
        //获取总共资源
        log.info("获取总共资源----");
        HdfsResourceModel hdfsAllResource = getHdfsAllResource(req, flag);
        return "{\"status\":\"ok\"}";
    }
}
